package com.zsy.controller;

import com.zsy.pojo.Category;
import com.zsy.pojo.Page;
import com.zsy.pojo.R;
import com.zsy.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired(required = false)
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody Category category, HttpSession session){
        Long result = (Long) session.getAttribute("result");

        category.setCreateUser(result);
        category.setUpdateUser(result);

        categoryService.save(category);

        return R.success("添加成功");
    }

   @GetMapping("/page")
    public R<Page<Category>> page(Integer page,Integer pageSize){
        Page<Category> pageResult = categoryService.page(page,pageSize);

        return R.success(pageResult);

   }

   @DeleteMapping()
    public R<String> delete(long id){

        categoryService.delete(id);

        return R.success("删除成功");
   }

   @PutMapping
    public R<String> update(@RequestBody Category category,HttpSession session){
       long result = (long) session.getAttribute("result");
       category.setUpdateUser(result);
       categoryService.update(category);

       return R.success("修改成功");
   }
   @GetMapping("/list")
    public R<List<Category>> list(Integer type){
        List<Category> categoryList = categoryService.findByType(type);
        return R.success(categoryList);
   }

}
